<template>
  <div class="roleConfig">
    <config-composition-two-window-style
      :leftWidth="8"
      :rightWidth="16"
      :height="125"
    >
      <template slot="card">
        <search
          :isReloadIvr="false"
          @add="add"
          @refresh="roleRefresh"
          @turnPage="turnPage"
          :currentPage="currentPage"
          :count="count"
          :pageSize="pageSize"
          :isShowInput="true"
          :inputTitle="$t('agentSetting.agentSetting41')"
          @search="rolesearch"
          :showAdvSearch="false"
        >
        </search>
        <el-table :data="tableData" size="mini">
          <el-table-column prop="display_name" :label="$t('agentSetting.agentSetting42')">
          </el-table-column>
          <el-table-column prop="memo" :label="$t('agentSetting.agentSetting43')">
          </el-table-column>
          <el-table-column :label="$t('public.operate')">
            <template scope="scope">
              <el-button @click="modify(scope.row)" type="text" size="small" class="update">{{$t('public.edit')}}</el-button>
              <el-button type="text" @click="delrow(scope.row)" size="small" class="del">{{$t('public.delete')}}</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template slot="detail">
        <role-edit v-show="showRole" :data="currentRole" @close="editClose" @saveDone="saveDone"></role-edit>
      </template>
    </config-composition-two-window-style>
  </div>
</template>

<script>
  import configCompositionTwoWindowStyle from './../../../ui-modules/config/configCompositionTwoWindowStyle'
  import breadCrumb from './../../../ui-modules/breadCrumb/BreadCrumbConfig.vue'
  import search from './../channelConfig/base/search.vue'
  import roleEdit from './roleEdit.vue'
  import {deepClone} from '@/utils/m7Utils'
  export default {
    name: 'roleConfig',
    data () {
      return {
        showRole: false,
        currentPage: 1,
        count: 0,
        pageSize: 10,
        tableData: [],
        currentRole: '',
        searchContent: ''
      }
    },
    methods: {
      rolesearch (val) {
        this.currentPage = 1
        this.searchContent = val
        this.fetchData()
      },
      roleRefresh () {
        this.currentPage = 1
        this.searchContent = ''
        this.fetchData()
      },
      saveDone () {
        this.showRole = false
        this.fetchData()
      },
      editClose () {
        this.showRole = false
      },
      delrow (row) {
        this.$confirm(this.$t('agentSetting.agentSetting44'), this.$t('public.delete'), {
          confirmButtonText: this.$t('public.delete'),
          cancelButtonText: this.$t('public.cancel'),
          cancelButtonClass: 'primary_btn',
          confirmButtonClass: 'primary_btn',
          type: 'warning'
        }).then(() => {
          this.$store.dispatch('delRole', {_id: row._id}).then((req) => {
            if (req.success) {
              this.$message.success(this.$t('message.deleteSucc'))
              this.fetchData()
            }
          })
        })
      },
      modify (row) {
        this.showRole = true
        this.currentRole = deepClone(row)
      },
      add () {
        this.showRole = true
        this.currentRole = ''
      },
      turnPage (val) {
        this.currentPage = val
        this.fetchData()
      },
      fetchData () {
        let data = {
          'page': this.currentPage,
          'limit': this.pageSize
        }
        if (this.searchContent !== '') {
          data.display_name = this.searchContent
        }
        this.$store.dispatch('queryRole', data).then((res) => {
          if (res.success) {
            this.tableData = res.list
            this.count = res.count || 0
          }
        })
      }
    },
    mounted () {
      this.fetchData()
    },
    components: {
      configCompositionTwoWindowStyle,
      breadCrumb,
      search,
      roleEdit
    }
  }
</script>

<style lang="stylus" scoped>
  @import "../../../../assets/common.styl"
  .update
    cursor pointer
  .el-table .cell >.el-button--text.del
    color $c-pink
    cursor pointer
  .roleConfig
    padding 16px
    height calc(100vh - 125px)
    overflow-y auto
</style>
